package com.rs.controller;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.rs.model.CommonResult;
import com.rs.model.ZhClasses;
import com.rs.model.ZhDepartment;
import com.rs.model.ZhPosition;
import com.rs.model.ZhUser;
import com.rs.service.PublicUserMethodsService;
import com.rs.service.ZhUserStudentService;
import com.rs.service.impl.PublicUserMethodsImpl;
import com.rs.service.impl.ZhUserStudentServiceImpl;
import com.rs.util.DateChooserJButton;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

import javax.swing.JCheckBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

@SuppressWarnings("all")
public class UserInfo extends JFrame {
	
	JDesktopPane desktopPane_1;
	
	private ZhUserStudentService zhUserService  = SpringContextUtils.getBean(ZhUserStudentService.class);
	
	private DateFormatUtil dd  = SpringContextUtils.getBean(DateFormatUtil.class);
	
	int  sum=0;  //为0表示为添加 为1表示为查询
	int  state=0; //1为修改状态 0为添加
	int  id; 

	private JPanel contentPane;
	private JTextField text_username;
	private JTextField text_pass;
	private JTextField text_name;
	private JTextField text_phone;
	private JTextField text_card;
	private JTextField text_sex;
	private JTextField text_age;
	private JTextField text_addess;
	private JTextField text_de;
	private JTextField text_op;
	private JTextField text_day;
	private JPanel panel_1;
	private JComboBox comboBox_de;
	private JComboBox comboBox_po;
	private JComboBox comboBox_class;
	private JComboBox combo_studyType ;
	private JButton btnNewButton;
	
	Map<String, Integer> mapde = new HashMap<>();
	Map<String, Integer> mappo = new HashMap<>();
	Map<String, Integer> mapca = new HashMap<>();
	
	private PublicUserMethodsService pum  = SpringContextUtils.getBean(PublicUserMethodsService.class);
	
	 DateChooserJButton dc= new DateChooserJButton();
	
	JLabel lblUusernameUpasswordUnameuphone;
	
	SpinnerDateModel model = new SpinnerDateModel();
     //���JSPinner����
     JSpinner year = new JSpinner(model);
     private JTextField text_class;
     private JLabel label_12;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserInfo frame = new UserInfo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserInfo() {
		
		aa();
	}
	public UserInfo( JDesktopPane desktopPane_1) {
		this.desktopPane_1=desktopPane_1;
		aa();
	}
	public UserInfo(int sum,int id,final JDesktopPane desktopPane_1) {
		this.desktopPane_1=desktopPane_1;
		this.sum=sum;
		this.id=id;
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				SelectUser user = new SelectUser();
				user.setVisible(true);
				desktopPane_1.removeAll();
				desktopPane_1.add(user);
			}
		});
		aa();
	} 

	void aa(){
	//	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblUusernameUpasswordUnameuphone = new JLabel("用户信息");
		lblUusernameUpasswordUnameuphone.setHorizontalAlignment(SwingConstants.CENTER);
		lblUusernameUpasswordUnameuphone.setFont(new Font("����", Font.PLAIN, 18));
		lblUusernameUpasswordUnameuphone.setBounds(234, 24, 131, 27);
		contentPane.add(lblUusernameUpasswordUnameuphone);
	
		
		JLabel label = new JLabel("\u8D26\u53F7\uFF1A");
		label.setBounds(45, 82, 54, 15);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("\u5BC6\u7801\uFF1A");
		lblNewLabel.setBounds(45, 136, 54, 15);
		contentPane.add(lblNewLabel);
		
		JLabel label_1 = new JLabel("\u59D3\u540D\uFF1A");
		label_1.setBounds(45, 188, 54, 15);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("\u624B\u673A\u53F7\uFF1A");
		label_2.setBounds(45, 247, 54, 15);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("\u8EAB\u4EFD\u8BC1\u53F7\uFF1A");
		label_3.setBounds(45, 301, 67, 15);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("\u6027\u522B\uFF1A");
		label_4.setBounds(45, 354, 54, 15);
		contentPane.add(label_4);
		
		JLabel label_5 = new JLabel("\u5E74\u9F84;");
		label_5.setBounds(45, 412, 54, 15);
		contentPane.add(label_5);
		
		text_username = new JTextField();
		text_username.setBounds(114, 79, 148, 27);
		contentPane.add(text_username);
		text_username.setColumns(10);
		
		text_pass = new JTextField();
		text_pass.setColumns(10);
		text_pass.setBounds(114, 130, 148, 27);
		
		contentPane.add(text_pass);
		
		text_name = new JTextField();
		text_name.setColumns(10);
		text_name.setBounds(114, 182, 148, 27);
		contentPane.add(text_name);
		
		text_phone = new JTextField();
		text_phone.setColumns(10);
		text_phone.setBounds(114, 244, 148, 27);
		contentPane.add(text_phone);
		
		text_card = new JTextField();
		text_card.setColumns(10);
		text_card.setBounds(114, 295, 148, 27);
		contentPane.add(text_card);
		
		text_sex = new JTextField();
		text_sex.setColumns(10);
		text_sex.setBounds(114, 348, 148, 27);
		contentPane.add(text_sex);
		
		text_age = new JTextField();
		text_age.setColumns(10);
		text_age.setBounds(114, 400, 148, 27);
		contentPane.add(text_age);
		
		JLabel label_6 = new JLabel("\u4F4F\u5740\uFF1A");
		label_6.setBounds(338, 82, 54, 15);
		contentPane.add(label_6);
		
		JLabel label_8 = new JLabel("\u90E8\u95E8\uFF1A");
		label_8.setBounds(338, 188, 54, 15);
		contentPane.add(label_8);
		
		JLabel label_9 = new JLabel("\u804C\u52A1\uFF1A");
		label_9.setBounds(338, 247, 54, 15);
		contentPane.add(label_9);
		
		JLabel label_10 = new JLabel("\u51FA\u751F\u65E5\u671F\uFF1A");
		label_10.setBounds(338, 354, 69, 15);
		contentPane.add(label_10);
		
		JLabel label_11 = new JLabel("班级：");
		label_11.setBounds(338, 301, 54, 15);
		contentPane.add(label_11);
		
		
 		
 		if(sum==0){
 			
 			 panel_1 = new JPanel();
 	 		 panel_1.setBounds(402, 348, 148, 27);
 	 		 contentPane.add(panel_1);
 	 		 panel_1.add(dc);
 	 		   
 	 		 year.setValue(new Date());	    
 	         JSpinner.DateEditor editor = new JSpinner.DateEditor(year,"yyyy-MM-dd");
 	         year.setEditor(editor);
 	         year.setBounds(402, 298, 148, 27);

 				comboBox_de = new JComboBox();
 				comboBox_de.setFont(new Font("等线", Font.PLAIN, 18));
 				comboBox_de.setBounds(402, 185, 146, 27);
 				comboBox_de.addItem("无");
 				contentPane.add(comboBox_de);
 				 List<ZhDepartment> de= pum.selectDeAll();
 				
				for(ZhDepartment dd:de){
				comboBox_de.addItem(dd.getDeName());
				mapde.put(dd.getDeName(),dd.getDeId());
				}
 				
 					
 				comboBox_po = new JComboBox();
 				comboBox_po.setFont(new Font("等线", Font.PLAIN, 18));
 				comboBox_po.setBounds(402, 241, 146, 27);
 				contentPane.add(comboBox_po);
 				comboBox_po.addItem("无");
 				List<ZhPosition> po= pum.selectPoAll();
 				
				for(ZhPosition zp:po){
				comboBox_po.addItem(zp.getPoName());
				mappo.put(zp.getPoName(),zp.getPoId());
				}
 				
 				
 				comboBox_class = new JComboBox();
 				comboBox_class.setBounds(402, 295, 148, 27);
 				contentPane.add(comboBox_class);
 				comboBox_class.addItem("无");
 				List<ZhClasses> cl= pum.selectCaAll();
 			
				for(ZhClasses cc:cl){
				comboBox_class.addItem(cc.getClName());
				mapca.put(cc.getClName(),cc.getClId());
 			
 				}
 				
 				
				btnNewButton = new JButton("\u63D0\u4EA4");
				
 				if(state ==0){
 				btnNewButton.addActionListener(new ActionListener() {
 					public void actionPerformed(ActionEvent e) {
 						
 						int b = pum.nameRepetition(text_username.getText());//
 						if(b>0){
 							alert("昵称一被用!");
 						}
 						if(!notNull()){
 							return;
 						}
 						ZhUser user=new ZhUser();
 						user.setuUsername(text_username.getText());
 						user.setuPassword(text_pass.getText());
 						user.setuName(text_name.getText());
 						user.setuPhone(text_phone.getText());
 						user.setuIdcard(text_card.getText());
 						user.setuSex(text_sex.getText());
 						user.setuAge(Integer.parseInt(text_age.getText()));		
 						user.setuBirthday(dc.getDate());
 						user.setuAddress(text_addess.getText());
 						
 						if(comboBox_de.getSelectedItem() !="无"){
 							user.setuDeid(mapde.get(comboBox_de.getSelectedItem()));
 						}
 						if(comboBox_po.getSelectedItem() !="无"){
 							user.setuPoid(mappo.get(comboBox_po.getSelectedItem()));
 						}
 						if(comboBox_po.getSelectedItem() !="无"){
 							user.setuCaid(mapca.get(comboBox_class.getSelectedItem()));
 						}
 						
 						if(combo_studyType.getSelectedItem() !="无"){
 							if(combo_studyType.getSelectedItem() =="java"){
 								user.setuStudyType(1);
 							}else if(combo_studyType.getSelectedItem() =="php"){
 								user.setuStudyType(2);
 							}
 						}
 						
 						 CommonResult<Integer> a =zhUserService.addStudent(user);
 						if(a.getData()>0){
 							alert("添加成功！");
 							close();
 						}else{
 							alert("添加失败！");
 						}
 					}
 				});
 				
 				
 				}
 				btnNewButton.setBounds(402, 408, 131, 41);
 				contentPane.add(btnNewButton);
 	 		  
 		}
		
		text_addess = new JTextField();
		text_addess.setColumns(10);
		text_addess.setBounds(402, 79, 148, 27);
		contentPane.add(text_addess);
		
		JLabel label_studyType = new JLabel("专业类型：");
		label_studyType.setBounds(338, 136, 67, 15);
		contentPane.add(label_studyType);
		
		combo_studyType = new JComboBox();
		combo_studyType.setModel(new DefaultComboBoxModel(new String[] {"无", "java", "php"}));
		combo_studyType.setBounds(402, 130, 148, 27);
		contentPane.add(combo_studyType);
		
		
		

		
		if(sum==1){
			text_day = new JTextField()	;	
 	 		text_day.setColumns(10);
 	 		text_day.setBounds(402, 348, 148, 27);
 	 		contentPane.add(text_day);
			
			text_de = new JTextField();
			text_de.setColumns(10);
			text_de.setBounds(402, 185, 148, 27);
			contentPane.add(text_de);
			
			text_class = new JTextField();
			text_class.setColumns(10);
			text_class.setBounds(402, 295, 146, 27);
			contentPane.add(text_class);
			
			JButton Teacher_del = new JButton("点击删除");
			Teacher_del.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {		
					int a = pum.userDel(id);
					alert(a>0?"删除成功":"删除失败");
					close();
					
				}
			});
			Teacher_del.setBounds(338, 399, 95, 40);
			contentPane.add(Teacher_del);
			
			JButton Teacher_upd = new JButton("点击修改");
			Teacher_upd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					sum=0;
					state=1;
					aa();
					ZhUser zh =pum.selectUserId(id);
					if(zh ==null){
						alert("查无此人！！");
						close();
					}
					
					text_username.setText(zh.getuUsername());
					text_pass.setText(text_pass.getText());
					text_name.setText(zh.getuName());
					text_phone.setText(zh.getuPhone());
					text_card.setText(zh.getuIdcard());
					text_sex.setText(zh.getuSex());
					text_age.setText(zh.getuAge()+"");
					text_addess.setText(zh.getuAddress());
					
					comboBox_de.setSelectedItem(pum.selectDe(zh.getuDeid()));
					comboBox_po.setSelectedItem(pum.selectPo(zh.getuPoid()));
					if(zh.getuStudyType() !=null &&zh.getuStudyType()==1){
						combo_studyType.setSelectedItem("java");
					}else if(zh.getuStudyType() !=null && zh.getuStudyType()==2){
						combo_studyType.setSelectedItem("php");
					}
					
					if(zh.getuCaid() !=null){
					text_class.setText(pum.selectCl(zh.getuCaid()));
					}
					if(zh.getuBirthday()  !=null){
						dc.setDate(zh.getuBirthday());
					}
						
				
		 	 		
		 	 		lblUusernameUpasswordUnameuphone.setText("修改信息");
		 	 		btnNewButton.addActionListener(new ActionListener() {
	 					public void actionPerformed(ActionEvent e) {
	 						ZhUser user=new ZhUser();
	 						user.setuId(id);
	 						user.setuUsername(text_username.getText());
	 						user.setuPassword(text_pass.getText());
	 						user.setuName(text_name.getText());
	 						user.setuPhone(text_phone.getText());
	 						user.setuIdcard(text_card.getText());
	 						user.setuSex(text_sex.getText());
	 						user.setuAge(Integer.parseInt(text_age.getText()));
	 						user.setuAddress(text_addess.getText());
	 						
	 						user.setuDeid(mapde.get(comboBox_de.getSelectedItem()));
	 						user.setuPoid(mappo.get(comboBox_po.getSelectedItem()));
	 						user.setuCaid(mapca.get(comboBox_class.getSelectedItem()));
	 						
	 						CommonResult<Integer> a= zhUserService.updateStudent(user);
	 						alert(a.getData()>0?"修改成功":"修改失败");
	 						close();
	 						final ZhUser zh =pum.selectUserId(id);
	 						SelectUser user2 = new SelectUser(zh.getuPoid(),desktopPane_1);
							user2.setVisible(true);
							desktopPane_1.removeAll();
							desktopPane_1.add(user2);
	 					}
		 	 		});
			
					
					
				}
			});
			Teacher_upd.setBounds(459, 399, 95, 40);
			contentPane.add(Teacher_upd);
			
			text_op = new JTextField();
			text_op.setBounds(402, 244, 148, 27);
			contentPane.add(text_op);
			text_op.setColumns(10);
		}
			
			
		if(id != 0 && sum==1){
			selectTeacherId();
		}
		
	}
	
	public  boolean notNull(){
		if(text_username.getText() ==null || text_username.getText().length()==0){
			alert("账号不能为空!");
			return false;
		}
		if(text_pass.getText() ==null || text_pass.getText().length()==0){
			alert("密码不能为空!");
			return false;
		}
		if(text_name.getText() ==null || text_name.getText().length()==0){
			alert("姓名不能为空!");
			return false;
		}
		if(text_phone.getText() ==null || text_phone.getText().length()==0){
			alert("手机号不能为空!");
			return false;
		}
		if(text_card.getText() ==null || text_card.getText().length()==0){
			alert("身份证号不能为空!");
			return false;
		}
		if(text_sex.getText() ==null || text_sex.getText().length()==0){
			alert("性别不能为空！");
			return false;
		}
		if(text_age.getText() ==null || text_age.getText().length()==0){
			alert("年龄不能为空!");
			return false;
		}
		if(text_addess.getText() ==null || text_addess.getText().length()==0){
			alert("地址不能为空!");
			return false;
		}
		return true;
	}
	
	
	
	
	
	public void selectTeacherId(){
		
		ZhUser zh =pum.selectUserId(id);
		
		text_username.setText(zh.getuUsername());
		text_pass.setText(zh.getuPassword());
		text_name.setText(zh.getuName());
		text_phone.setText(zh.getuPhone());
		text_card.setText(zh.getuIdcard());
		text_sex.setText(zh.getuSex());
		text_age.setText(zh.getuAge()+"");
		if(zh.getuBirthday()!=null){
			text_day.setText(dd.getDateString(zh.getuBirthday(),"yyyy-HH-mm"));
		}
		text_addess.setText(zh.getuAddress());		
		
		if(zh.getuDeid() !=null){
			text_de.setText(pum.selectDe(zh.getuDeid()));
		}
		if( zh.getuPoid()!=null){
			text_op.setText(pum.selectPo(zh.getuPoid()));
		}
		if(zh.getuCaid() !=null){
			System.out.println(zh.getuCaid());
			text_class.setText(pum.selectCl(zh.getuCaid()));
		}
		
		text_username.setEditable(false);
		text_pass.setEditable(false);
		text_name.setEditable(false);
		text_phone.setEditable(false);
		text_card.setEditable(false);
		text_sex.setEditable(false);
		text_age.setEditable(false);
		text_de.setEditable(false);
		text_op.setEditable(false);
		text_day.setEditable(false);
		text_addess.setEditable(false);
		text_class.setEditable(false);
	}
	public void alert(String alert){
		JOptionPane.showMessageDialog(this, alert);
	}

	/**
	 * Create the frame.
	 */
	

	void close(){
		
		this.dispose();
	}
}
